var abc = {}

var thisURL = window.location.host;
var downloadSuffix = '';
var downloadUrl = ''
if (thisURL == 'store.op.intra.meiktv.com') {
    hostUrl = 'http://store.api.intra.meiktv.com/';
    downloadSuffix = '_intra';
} else if (thisURL == 'store.op.pre.meiktv.com') {
    hostUrl = 'http://store.api.pre.meiktv.com/';
    downloadUrl = 'http://store.pre.meiktv.com'
    downloadSuffix = '_pre';
} else if (thisURL.match('store.op.meiktv.com')) {
    hostUrl = 'https://store.api.meiktv.com/';
    downloadUrl = 'http://store.meiktv.com'
    downloadSuffix = '';
} else {
    hostUrl = 'http://store.api.pre.meiktv.com/';
    downloadSuffix = '_pre';
}
abc.hostUrl = hostUrl;
abc.downloadSuffix = downloadSuffix;
abc.downloadUrl = downloadUrl;


//设置session
function setSession(key, value) {
    if (typeof value == "object") {
        value = JSON.stringify(value)
    }
    sessionStorage.setItem(key, value);
}
//读取session
function getSession(key) {
    var value = sessionStorage.getItem(key);

    return value;
}

//删除session
function deleteSession() {
    sessionStorage.clear();
}
//判断token
// judgeToken()

// function judgeToken() {
//     if (!sessionStorage.getItem('token') && window.location.href.indexOf('login') == -1) {
//         deleteSession();
//         top.location.href = "/login/index.html";
//     }
// }
//设置公参






abc.setSession = setSession;
abc.getSession = getSession; 
abc.deleteSession = deleteSession; 
// post请求
 function post(_this,url,param,cb,led) {
    var publicData = abc.getSession('params'),urls=''
    if(led == 'led'){
        urls = url
    }else{
        urls = abc.hostUrl+url+publicData
    }
    _this.$http.post(urls,param,{emulateJSON: true}).then( (res_res) => {
        var res = res_res.body;
        if(res.code == 200){
            cb(res)
        }else{
            if(res.code == '100115' || res.code == '100114'){
                setTimeout(() => {
                    _this.$router.push('/login')
                }, 2000);
            }
            _this.$notify.info({
                title: '提示',
                message: res.message
            });
        }
    }, (err) => {
        _this.$notify.error({
                title: '错误',
                message: '系统错误，稍后尝试~devicelogin'
            });
    })
}
abc.post = post;
// get请求
function get(_this,url,param,cb) {
    var publicData = abc.getSession('params')
    var params = param;
    params.page = _this.pageNum
    params.size = '20'
    _this.$http.get(abc.hostUrl+url+publicData,{params:params},{emulateJSON: true}).then( (res_res) => {
        var res = res_res.body;
        if(res.code == 200){
            cb(res)
        }else{
            if(res.code == '100115' || res.code == '100114'){
                setTimeout(() => {
                    _this.$router.push('/login')
                }, 2000);
            }
            _this.$notify.info({
                title: '提示',
                message: res.message
            });
        }
    }, (err) => {
        _this.$notify.error({
                title: '错误',
                message: '系统错误，稍后尝试~devicelogin'
            });
    })
}
abc.get = get;
// 每页列表
function getList(that,url,data,cb=function(){}) {
    // var publicData = abc.getSession('params')
    var params = data;
    // params.page = that.pageNum
    params.size = '20'
    get(that,url,params,function(res){
        console.log(res.data.list)
        if(res.data.list != undefined){
            that.total =parseInt(res.data.total)
            that.tableData = res.data.list;   
        }else{
            that.total =parseInt(res.data.length)
            that.tableData = res.data; 
        }
        
        // if()
        cb(res.data.list)
    })
}

abc.getList = getList;
// 权限
function limit(that,str){
    console.log('bbn',str)
    var needStr = str.slice(1);
    var list = JSON.parse(abc.getSession('menu_list')) 
    for(var i=0;i<list.length;i++){
        var subList = list[i].sub_menu
        if(subList.length != 0){
            for(var sub=0;sub<subList.length;sub++){
                var subItem = subList[sub]
                if(subItem.url.indexOf(needStr) != -1){
                    if(subItem.sub_menu.length>0){
                        that.limitList = subItem.sub_menu;
                        
                    }
                    
                    
                }
            }
        }
    }
    
}
abc.limit = limit;
// 二次确认的方法
function twoTips(that,url,txt,pars,cb) {
    that.$confirm(txt, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        post(that,url,pars,cb)
                
      }).catch(() => {
                  
      });
}
abc.twoTips = twoTips;
// 价格进位或不进位最后位数为8
function ModifyPrice(org) {
    if (org)
        org = Math.floor(org);
    var ten = parseInt(org / 10);
    var bit_a = org % 10;
    //根据个位数判断；
    if (bit_a < 5 && ten > 9) {
        ten = ten - 1;
    }
    //根据十位数做判断；
    var bit_b = ten % 10;
    if (bit_b == 0 && ten > 10) {
        ten = ten - 1
    } else if (bit_b == 4) {
        ten = ten + 1
    }
    return (ten * 10 + 8)
}
abc.ModifyPrice = ModifyPrice

function getpremonth() {
    let dateobj = new Date()
    let m = dateobj.getMonth()>9?dateobj.getMonth():'0'+dateobj.getMonth()
    return (dateobj.getFullYear() +'-'+ m)
}

abc.getpremonth = getpremonth
function getStore(that) {
    abc.get(that,'v1/store/getuserstorelist',{},function(res) {
        console.log('store',res)
        that.storeList = res.data
    })//获取门店列表-
}
abc.getStore = getStore;
//获取员工列表
function  getStaff(that) {
    abc.get(that,'operate/staff/list',{},(res)=>{
        that.staffList = res.data
    })
}
abc.getStaff = getStaff
//包含店长和门店员工的列表
function  storeStaff(that,cb=null) {
    abc.get(that,'v2/store/staff',{},(res)=>{
        that.storeStaff = res.data;
        if(cb != null){
            cb()
        }
    })
}
abc.storeStaff = storeStaff
	//正整数验证
function zzNum(that,str){

	var reg=/^[0-9]+\d*$/; //含0正整数
	console.log(str)
	if(!reg.test(str)){
		that.$message('请填写大于零，并且是整数的数字！')
		return false;		
	}else{
		return true;
		
	}
}
abc.zzNum = zzNum
export { abc }